# added by yinchao, Tue June 25 15:37	2024
import socket
import re

address = input('Enter an address(default http://data.pr4e.org/romeo.txt) ')
if address == '':
    address = 'http://data.pr4e.org/romeo.txt'


try:
    hostname, arg = re.search('http[s]?://(.+?)/(.+)', address).groups()
except Exception as e:
    print(f'The format of address is error, {e}')
    exit()

print(hostname, arg)

mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    mysocket.connect((hostname, 80))
except Exception as e:
    print('can\'t connected to the hostname' + hostname)
    exit()
cmd = f'GET {address} HTTP/1.0\r\n\r\n'
cmd = bytes(cmd, 'ascii')
mysocket.sendall(cmd)

content = b''
while True:
    data = mysocket.recv(4096)
    if len(data) < 1:
        break
    content += data

mysocket.close()
print(content.decode())
